#pragma once

#include "CQJson.hpp"
#include <string>
#include <iostream>
#include <cassert>
using namespace std;
using namespace CQJson;

string jsonstring = "\
{\
	\"name\":\"dqsjqian\",\
	\"age\" : 26.9,\
	\"data\" : [\
	{\
		\"one\":\"CQReflectJson\",\
		\"two\" : {\
			\"key1\":\"2233\",\
			\"key2\":44444\
		}\
	},\
	{\
		\"one\":\"CQReflectJson2\",\
		\"two\" : {\
			\"key1\":\"8899\",\
			\"key2\":10000\
		}\
	}\
	],\
	\"lang\":\"2cpp\"\
}\
";

string jsonstring2 = "\
{\
	\"name\":\"dqsjqian\",\
	\"age\" : 26.9,\
	\"data\" : [\
	{\
		\"one\":\"CQReflectJson\",\
		\"two\" : [\
			\"hehe\",\
			\"haha\"\
		]\
	},\
	{\
		\"one\":\"CQReflectJson2\",\
		\"two\" : [\
			\"hehe2\",\
			\"haha2\"\
		]\
	}\
	],\
	\"lang\":\"cpp\"\
}\
";

string jsonstring3 = "\
{\
	\"xx\": {\
		\"a\": 1,\
		\"b\" : 2\
	}\
}";

// read demo
void Test0() {
	cout << "\nTest 0 read json" << endl;
	// read
	CQTinyJson json;
	json.ReadJson(jsonstring3);

	CQObject data = json.Get<CQObject>("xx");
	for (int i = 0; i < data.Count(); i++) {
		data.Enter(i);
		int one = data.Get<int>("a");
		int two = data.Get<int>("b");
		int three = data.Get<int>("c", 99);
		assert(one == 1);
		assert(two == 2);
		assert(three == 99);
	}
	cout << "Test 0 pass" << endl;
}


// read demo
void Test1() {
	cout << "\nTest 1 read json" << endl;
	// read
	CQTinyJson json;
	json.ReadJson(jsonstring);

	string name = json.Get<string>("name");
	float age = json.Get<float>("age");
	string lang = json.Get<string>("lang");

	assert(name == "dqsjqian");
	assert(age > 26 && age < 27);
	assert(lang == "2cpp");

	CQArray data = json.Get<CQArray>("data");
	for (int i = 0; i < data.Count(); i++) {
		data.Enter(i);
		string one = data.Get<string>("one");
		if (i == 0)
		{
			assert(one == "CQReflectJson");
		}
		if (i == 1)
		{
			assert(one == "CQReflectJson2");
		}
		CQObject two = data.Get<CQObject>("two");
		for (int ii = 0; ii < two.Count(); ii++) {
			two.Enter(ii);
			string key1 = two.Get<string>("key1");
			int key2 = two.Get<int>("key2");
			if (i == 0)
			{
				assert(key1 == "2233");
				assert(key2 == 44444);
			}
			if (i == 1)
			{
				assert(key1 == "8899");
				assert(key2 == 10000);
			}

		}
	}
	cout << "Test 1 pass" << endl;
}

// read demo
void Test2() {
	cout << "\nTest 2 read json" << endl;
	// read
	CQTinyJson json;
	json.ReadJson(jsonstring2);

	string name = json.Get<string>("name");
	float age = json.Get<float>("age");
	string lang = json.Get<string>("lang");

	assert(name == "dqsjqian");
	assert(age > 26 && age < 27);
	assert(lang == "cpp");

	CQArray data = json.Get<CQArray>("data");
	for (int i = 0; i < data.Count(); i++) {
		data.Enter(i);
		string one = data.Get<string>("one");
		if (i == 0)
		{
			assert(one == "CQReflectJson");
		}
		if (i == 1)
		{
			assert(one == "CQReflectJson2");
		}
		CQArray two = data.Get<CQArray>("two");
		for (int ii = 0; ii < two.Count(); ii++) {
			two.Enter(ii);
			string val = two.GetKey();
			if (i == 0 && ii == 0)
			{
				assert(val == "hehe");
			}
			if (i == 0 && ii == 1)
			{
				assert(val == "haha");
			}
			if (i == 1 && ii == 0)
			{
				assert(val == "hehe2");
			}
			if (i == 1 && ii == 1)
			{
				assert(val == "haha2");
			}
		}
	}
	cout << "Test 2 pass" << endl;
}

void Test3() {
	cout << "\nTest 3 write json" << endl;
	// write
	CQTinyJson wjson;
	wjson["name"].Set("dqsjqian");
	wjson["age"].Set(26);
	wjson["handsome"].Set(true);

	CQTinyJson subjson;
	subjson["key1"].Set("book");
	subjson["key2"].Set(666);

	CQTinyJson subjson2;
	subjson2["key1"].Set("book2");
	subjson2["key2"].Set(6662);

	wjson["data"].Set(subjson);
	wjson["data2"].Set(subjson2);

	string str = wjson.WriteJson();
	cout << "Json string:" << endl;
	cout << str << endl;
	cout << "Test 3 pass" << endl;
}

void Test4() {
	cout << "\nTest 4 write json" << endl;
	// write
	CQTinyJson wjson;
	wjson["name"].Set("dqsjqian");
	wjson["age"].Set(26);
	wjson["handsome"].Set(true);

	CQTinyJson subjson;
	subjson["key1"].Set("book");
	subjson["key2"].Set(666);

	CQTinyJson subjson2;
	subjson2["key1"].Set("book2");
	subjson2["key2"].Set(6662);

	CQTinyJson jsonarray;
	jsonarray.Push(subjson);
	jsonarray.Push(subjson2);

	wjson["data"].Set(jsonarray);

	string str = wjson.WriteJson();
	cout << "Json string:" << endl;
	cout << str << endl;
	cout << "Test 4 pass" << endl;
}

void Test5() {
	cout << "\nTest 5 write json" << endl;
	// write
	CQTinyJson wjson;
	wjson["name"].Set("dqsjqian");
	wjson["age"].Set(26);
	wjson["handsome"].Set(true);

	CQTinyJson subjson;
	subjson[""].Set("book");
	subjson[""].Set(666);

	CQTinyJson jsonarray;
	jsonarray.Push(subjson);

	wjson["data"].Set(jsonarray);

	string str = wjson.WriteJson();
	cout << "Json string:" << endl;
	cout << str << endl;
	cout << "Test 5 pass" << endl;
}
